package com.myadapter;

import com.bean.article;
import com.example.lizhiinformation.R;
import com.tools.ImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FrontPage_information_adapter extends BaseAdapter {
	private Context context;
	private article article;
	private View view;
	private LayoutInflater inflater;
	private final static int INFORMATION1=0;
	private final static int INFORMATION2=1;

	public FrontPage_information_adapter(Context context, com.bean.article article,View view) {
		super();
		this.context = context;
		this.article = article;
		this.view=view;
		 inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return article.getContents().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return article.getContents();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}
	@Override
	public int getItemViewType(int position) {
		if(article.getContents().get(position).getCategory().equals("txt")){
			return INFORMATION1;	
		}else {
			return INFORMATION2;
		}
			
		
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		switch (getItemViewType(position)) {
		case INFORMATION1:
			ViewHolderText holderText=null;
			if(convertView==null){
			 convertView = inflater.inflate(R.layout.fromtpage_item_information_text, null);
			 holderText=new ViewHolderText();
			 holderText.textView=(TextView) convertView.findViewById(R.id.information_text);
			 convertView.setTag(holderText);
			}else {
				holderText=(ViewHolderText) convertView.getTag();
			}
			holderText.textView.setText("\n"+"\t"+"\t"+"\t"+"\t"+article.getContents().get(position).getText());
			break;
		case INFORMATION2:
			ViewHolderImage holderImage=null;
			if(convertView==null){
				convertView=inflater.inflate(R.layout.fromtpage_item_information_image, null);
				holderImage=new ViewHolderImage();
				holderImage.imageView=(ImageView) convertView.findViewById(R.id.information_image);
				convertView.setTag(holderImage);
			}else {
				holderImage=(ViewHolderImage) convertView.getTag();
			}
			String link = article.getContents().get(position).getLink();
			
			ImageLoader.cachr(context, link, holderImage.imageView,view);
			break;
		default:
			break;
		}
		return convertView;
	}
	class ViewHolderText{
		private TextView textView;
	}
	class ViewHolderImage{
		private ImageView imageView;
	}
}
